<template>
  <div class="wrapdiv">
    <el-form :inline="true" :model="form_search" class="demo-form-inline">
      <el-form-item label="类型" class="formitemheader">
        <el-input v-model="form_search.logType" placeholder="调度">
        </el-input>
      </el-form-item>
      <el-form-item label="操作" class="formitemheader">
        <el-input v-model="form_search.logAction" placeholder="新增/更新/删除">
        </el-input>
      </el-form-item>
      <el-form-item label="日期" class="formitemheader">
        <el-date-picker
          v-model="form_search.dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item class="formitemheader">
        <el-button type="primary" @click="doQueryLog">查询</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col style="margin: 10px">
        <el-button type="danger" @click="deleteLogs">删除</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <el-table
          :header-cell-style="{
            color: '#111',
            backgroundColor: 'rgba(20,20,20,.1)',
            borderRadius: '2px',
            border: '1px solid #fff',
          }"
          ref="multipleTable"
          :data="logList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          @cell-click="cellclick"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column type="index"> </el-table-column>

          <el-table-column
            header-align="center"
            align="center"
            prop="log_type"
            label="类型"
            width="80"
          >
          </el-table-column>
          <el-table-column
            header-align="center"
            align="center"
            prop="log_action"
            label="操作"
            width="80"
          ></el-table-column>
          <el-table-column
            header-align="center"
            align="center"
            prop="detail"
            label="描述"
          >
          </el-table-column>
          <el-table-column
            header-align="center"
            align="center"
            prop="time"
            label="时间"
            width="160"
          >
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row>
      <el-col style="margin: 10px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="pageNum"
          :page-sizes="[10, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next"
          :total="total"
        >
        </el-pagination>
      </el-col>
    </el-row>
    <!-- {{form_search.dateRange ==null}} -->
  </div>
</template>

<script>
export default {
  name: "Log_list",
  data() {
    return {
      //[ "2021-05-10", "2021-06-06" ]
      form_search: {
        logType: "",
        logAction: "",
        dateRange: [],
        from: "",
        to: "",
      },

      pageNum: 1,
      pageSize: 10,
      total: 0,
      logList: [
        // {
        //   log_id: "1",
        //   log_type: "新增",
        //   log_action: "资源",
        //   detail: "新增资源xxx.mp4",
        //   time: "2021-5-6 11:20:30",
        // }
      ],
      multipleSelection: [],
    };
  },
  mounted() {
    this.getLogList();
  },
  methods: {
    deleteLogs() {
      this.$confirm("确定要删除选中的日志吗?", { type: "warning" })
        .then(async (_) => {
          ///
          if (this.multipleSelection.length) {
            let ids = this.multipleSelection.map((v) => v.log_id);
            //console.log(ids);
            let ret = await this.$http.delete("logs", {
              data: { log_ids: ids },
            });
            let { action, result } = ret.data.data;
            console.log(action, result);
            if (result == "ok") this.$message.success("删除日志成功");
            else this.$message.error("删除日志失败");
            this.getLogList();
          }
        })
        .catch((_) => {});
    },
    async getLogList() {
      let ret = await this.$http.get(
        `logsByPage?pageNum=${this.pageNum}&pageSize=${this.pageSize}`
      );
      let { pageNum, total, logs } = ret.data.data;
      this.pageNum = pageNum;
      this.total = total;
      this.logList = logs;
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
    cellclick(row, col, cell, event) {
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getLogList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.getLogList();
    },

    async doQueryLog() {
      console.log(this.form_search);
      if (
        this.form_search.dateRange &&
        this.form_search.dateRange.length == 2
      ) {
        this.form_search.from = this.form_search.dateRange[0];
        this.form_search.to = this.form_search.dateRange[1];
      } else {
        this.form_search.from = "";
        this.form_search.to = "";
      }

      let ret = await this.$http.get(
        `logsByPage?pageNum=${this.pageNum}&pageSize=${this.pageSize}&logType=${this.form_search.logType}&logAction=${this.form_search.logAction}&from=${this.form_search.from}&to=${this.form_search.to}`
      );

      let { pageNum, total, logs } = ret.data.data;
      this.pageNum = pageNum;
      this.total = total;
      this.logList = logs;
    },
  },
};
</script>

<style scoped>
.wrapdiv {
  width: 98%;
  border-radius: 10px;
  padding: 5px;
  box-shadow: 0 0 10px #aaa;
}
.formitemheader {
  margin: 10px;
}
</style>
